package com.fc.common.api.service.impl;

import com.fc.common.api.mapper.CommonMapper;
import com.fc.common.api.service.CommonAPI;
import com.fc.common.api.vo.Dict;
import com.fc.common.api.vo.DictItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CommonAPIImpl implements CommonAPI {
     @Resource
     private CommonMapper commonMapper;

     @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Override
    public String selectDict(Map<String, Object> map) {
        return commonMapper.getDict(map);
    }

    @Override
    public void initRedisDict() {
        List<Dict> dicts = commonMapper.selectDicts();
        for(Dict dict:dicts ){
            List<DictItem> items = commonMapper.selectDictByDictId(dict.getId());
            Map<String,String> map = items.stream().collect(Collectors.toMap(DictItem::getValue,DictItem::getText));
            redisTemplate.opsForHash().putAll(dict.getDicCode(),map);
        }
    }
}
